import os
import shutil

folder = 'C:/Users/' + os.getlogin() + '/AppData/Local/Temp'


def CleanTemp():
    deleteFileCount = 0
    deleteFolderCount = 0
    print('开始清理Temp文件夹--ByYingesetch')
    for the_file in os.listdir(folder):
        file_path = os.path.join(folder, the_file)
        indexNo = file_path.find('\\')
        itemName = file_path[indexNo + 1:]
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
                print('' % itemName)
                deleteFileCount = deleteFileCount + 1

            elif os.path.isdir(file_path):
                if file_path.__contains__('chocolatey'): continue
                shutil.rmtree(file_path)
                print('删除文件夹-> %s' % itemName)
                deleteFolderCount = deleteFolderCount + 1

        except Exception as e:
            print('拒绝访问，无法删除: %s' % itemName)
    print(folder + ' 文件夹中有 ' + str(deleteFileCount) + ' 个文件和 ' +
          str(deleteFolderCount) + ' 个文件夹被删除！！')
    return


if __name__ == "__main__":
    CleanTemp()
